<!doctype html>
<html lang="en" data-bs-theme="auto">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <title>分类管理</title>

  <link href="/assets/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/assets/icons/font/bootstrap-icons.css">
  <link rel="stylesheet" href="/libs/tree/select-tree.css">
  
  <link href="/css/dashboard.css" rel="stylesheet">
  <script src="/js/jquery.min.js"></script>
  <script src="/libs/tree/select-tree.js"></script>
</head>

<body>

  <div class="container">
  <h5>分类管理</h5>
  <hr>
  <div class="row p-3">
      <div class="col-10">
        <table class="table table-bordered">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">名称</th>
              <th scope="col">编辑</th>
            </tr>
          </thead>
          <tbody>
            <%c for(auto &a:obj["list"].as_array()){ %>
              <tr id="topicid_<%c echo<<a.second["id"].to_string(); %>">
                <td>
                  <%c echo<<a.second["id"].to_string(); %>
                </td>
                <td <%c if(a.second["parentid"].to_int()>0){ %>style="padding-left:<%c echo<<(a.second["level"].to_int()*15+15); %>px"<%c } %>  id="itemid_<%c echo<<a.second["id"].to_string(); %>">
                  <%c echo<<a.second["value"].as_string(); %>
                </td>
                 
                <td>
                  <a href="javascript:void()" data-cid="<%c echo<<a.second["id"].to_string(); %>"  data-pid="<%c echo<<a.second["parentid"].to_string(); %>" data-title="<%c echo<<a.second["value"].as_string(); %>" data-bs-toggle="modal" data-bs-target="#staticBackdrop">编辑</a>｜
                  <a href="/admin/deletecatalogue?id=<%c echo<<a.second["id"].to_string(); %>" onclick="return deletecatalogue(<%c echo<<a.second["id"].to_string(); %>)">删除</a>
                </td>
              </tr>
              <%c } %>
          </tbody>

        </table>
      </div>
    </div>


    <form class="row g-3 mt-1" id="form" method="post">
      
      <div class="col-md-6">
        <label for="parentid" class="form-label">父类别</label>
        <select class="form-select" id="parentid" name="parentid">
        </select>
      </div>
      <div class="col-md-6">
        <label for="topicname" class="form-label">名称</label>
        <input type="text" class="form-control" id="topicname" name="topicname">
      </div>
      <div class="col-12">
        <button type="submit" class="btn btn-primary">提交</button>
      </div>
    </form>

  </div>
  
  
   <div class="modal fade" id="staticBackdrop" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="exampleModalLabel">编辑名称</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
      <form id="editcategory" method="post" enctype="application/x-www-form-urlencoded">
       <div class="col-md-6">
        <label for="editparentid" class="form-label">父类别</label>
        <select class="form-select" id="editparentid" name="editparentid">
        </select>
      </div>
      <div class="col-md-6">
        <label for="editname" class="form-label">名称</label>
        <input type="text" class="form-control" id="editname" name="editname">
        <input type="hidden" id="cid" name="cid" value="0" />
      </div>
      </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" onclick="typepost()">Save</button>
      </div>
    </div>
  </div>
</div>
  
  
  <script src="/assets/dist/js/bootstrap.bundle.min.js"></script>
  <script lang="javascript">

    var topic_json = <% echo << obj["list"].to_json(); %> ;
    $('#parentid').append($('<option>').val("0").text("一级类别").attr("selected", true));
    for (var i = 0; i < topic_json.length; i++) {
          
       $('#parentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid));
       $('#editparentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid));
   }

    var treeselect = $("#parentid").selectTree({ expandLevel: 1 });
    var treeedit = $("#editparentid").selectTree({ expandLevel: 1 });

    $("#form").on("submit", function (event) {
      event.preventDefault();

      var data = $(this).serialize();

      var obj = this;

      //得到序列化数据
      $.ajax({
        url: "/admin/addcataloguepost",
        method: 'post',
        data: data,
        success: function (res) {
          //添加成功，这里演示
          console.log(res);
          if (res.code == 0) {
            topic_json = res.list;
            var pid=$('#parentid').val();
            $('#parentid').empty();

            $('#parentid').append($('<option>').val("0").text("一级类别"));
            for (var i = 0; i < topic_json.length; i++) {
            if(topic_json[i].id==pid)
              {
                 $('#parentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid).attr("selected", true));
              }
              else
              {
                  $('#parentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid));
        
              }
            }
              treeselect.selectOption(pid);
          }
          $("#form").trigger("reset");
        }
      })
    });
		$('#staticBackdrop').on('show.bs.modal', function (event) {

var button = $(event.relatedTarget) 

var pid = button.data('pid');
var pname = button.data('title');
var cid = button.data('cid');

$("#cid").val(cid);
$("#editname").val(pname);
$('#editparentid').empty();

 $('#editparentid').append($('<option>').val("0").text("一级类别"));
treeedit.clearSelect();

    for (var i = 0; i < topic_json.length; i++) {
      if(topic_json[i].id==pid)
      {
        $('#editparentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid).attr("selected", true));
      }
      else
      {      
        $('#editparentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid));
      }
   }
   
   treeedit.selectOption(pid);

});

   function typepost() {
      var data = $(editcategory).serialize();

      //得到序列化数据
      $.ajax({
        url: "/admin/editcataloguepost",
        method: 'post',
        data: data,
        success: function (res) {
          //添加成功，这里演示
          if (res.code == 0) {
              $("#itemid_"+$("#cid").val()).html($("#editname").val());
          }
          $('#staticBackdrop').modal('hide');
        }
      });
    }
    function deletecatalogue(id)
    {
      if(confirm('是否删除?'))
      {          
          $.getJSON("/admin/deletecatalogue?id="+id,function(result){
                if(result.code==0)
                {
                
                     $("#topicid_"+id).remove();
                }
                
          });

      }
      return false;
    }
  </script>
</body>

</html>